package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

public class JDBCDemo5 {
    public static void main(String[] args) {
        try (Connection conn = DBUtil.getConnection();
        ){
            Statement statement = conn.createStatement();
            String sql = "select t.name,t.salary,t.title,c.name,c.floor "+
                    "from teacher t " +
                    "JOIN class c ON t.id = c.teacher_id " +
                    "where t.name = '王克晶' ";
            System.out.println(sql);
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {

                //获取字段值时,可以利用字段索引,注意从1开始为结果集的第一个字段.可读性差,不推荐
                String tName = rs.getString("t.name");
                int tSalary = rs.getInt("t.salary");
                String tTitle = rs.getString("t.title");
                String cName = rs.getString("c.name");
                String cFloor = rs.getString("c.floor");
                System.out.println(tName + " " + tSalary + " " + tTitle + " " + cName + " " + cFloor);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    }

